﻿using System;
using Evpo.ContactData.Common;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace Evpo.ContactData.Data
{
    public class RelationshipAdapter
    {
        public RelationshipAdapter(Connection conn)
        {
            _conn = conn;
        }

        #region Fields
        private readonly Connection _conn;
        #endregion

        #region Properties
        #endregion

        #region Methods

        public void DeleteRelationship(int contactID, int relatedContactID)
        {
            var parameters = new List<SqlParameter>()
            {
                new SqlParameter("@ContactID", contactID),
                new SqlParameter("@RelatedContactID", relatedContactID),
            };

            using (var execProvider = new StatementExecutionProvider(
                _conn, SProcs.DeleteRelationship, parameters))
            {
                execProvider.ExecuteNonQuery();
            }
        }

        public List<RelationshipInfo> Relationships(int contactID)
        {
            var parameters = new List<SqlParameter>()
            {
                new SqlParameter("@ContactID", contactID),
            };
            
            var retVal = new List<RelationshipInfo>();
            using (var procReader =
                new ProcedureOutputReader(_conn, SProcs.Relationships, parameters))
            {
                SqlDataReader reader = procReader.CreateReader();
                while (reader.Read())
                {
                    int relatedContactID = procReader.GetID("RelatedContactID");

                    var contact = new Contact();
                    ContactAdapter.FillFieldsFromReader(contact, procReader);

                    retVal.Add(
                        new RelationshipInfo()
                        {
                            Relationship = new Relationship()
                            {
                                ContactID = contactID,
                                RelatedContactID = relatedContactID,
                                RelationshipType = procReader.GetID("RelationshipType")
                            },
                            Contact = contact,
                            TitleName = procReader.GetString("TitleName"),
                            TypeName = procReader.GetString("TypeName")
                        });
                }
            }
            return retVal;

        }

        public void SaveRelationship(Relationship relationship)
        {
            var parameters = new List<SqlParameter>()
            {
                new SqlParameter("@ContactID", relationship.ContactID),
                new SqlParameter("@RelatedContactID", relationship.RelatedContactID),
                new SqlParameter("@RelationshipType", relationship.RelationshipType)
            };

            using (var execProvider = new StatementExecutionProvider(
                _conn, SProcs.SaveRelationship, parameters))
            {
                execProvider.ExecuteNonQuery();
            }
        }

        public List<RelationshipType> RelationshipTypes()
        {
            var retVal = new List<RelationshipType>();
            using (var procReader =
                new ProcedureOutputReader(_conn, SProcs.RelationshipTypes))
            {
                SqlDataReader reader = procReader.CreateReader();
                while (reader.Read())
                {
                    retVal.Add(
                        new RelationshipType()
                        {
                            ID = procReader.GetID("ID"),
                            Name = procReader.GetString("Name")
                        });
                }
            }
            return retVal;
        }

        #endregion Methods

    }
}
